/*
 * WRITTEN BY: Thomas Jakway
 * Project GLOnHill
 * http://code.google.com/p/gl-on-hill/
 * anonymous SVN checkout:
 * svn checkout http://gl-on-hill.googlecode.com/svn/trunk/ gl-on-hill-read-only
 * 
 * Demonstrates drawing an effects-filled scene in C++ with OpenGL
 * Textures are fetched for educational purposes only
 *
 *
 * Licensed under the GNU GPL v3, available from http://www.gnu.org/licenses/gpl-3.0.txt
 */

#ifndef DRAW
#define DRAW


//standard C libraries
#include <stdio.h>
#include <stdlib.h>


#ifndef GLOBAl
#include "global.h"
#endif

//SDL - Simple Direct Layer for getting the OpenGL context
#include <SDL.h>

#ifdef MAC_OS_X
#include <OpenGL/OpenGL.h>
#include <OpenGL/glu.h>
#endif

#ifdef LINUX
#include <GL/gl.h>
#include <GL/glu.h>
#endif


//the Hill hierarchy classes
#ifndef HILL_MESH
#include "Hill_Mesh.h"
#endif
#ifndef HILL_CUBE
#include "Hill_Cube.h"
#endif
#ifndef HILL_PYRAMID
#include "Hill_Pyramid.h"
#endif
#ifndef HILL_BLOCK_GROUP
#include "Hill_Block_Group.h"
#endif

//inialization function and resize function
#ifndef RESIZE_AND_INIT
#include "resize_and_init.h"
#endif

//standard C++ library includes
#include <iostream>
#include <string>
using namespace std;

//function prototypes
void drawStarWall(int length);
void drawDoor();


//include the StarLine class to draw the walls
#include "StarLine.h"

//variables holding user input
extern float userrotatex, userrotatey, userrotatez;
extern float usertranslatex, usertranslatey, usertranslatez;

//the external array of texture ID's
extern GLuint tex_id[SIZE_OF_TEX_ARRAY];

static Hill_Block_Group Blocks[NUM_ALLOCATED_BLOCKS] =
{
	Hill_Block_Group(6,6),
	Hill_Block_Group(12 ,12),
	Hill_Block_Group(18,18),
	Hill_Block_Group(24,24 ),
	Hill_Block_Group(30, 30)
};

#define AMT_HOUSE_BLOCK_ALLOC 3
static Hill_Block_Group House_Blocks[AMT_HOUSE_BLOCK_ALLOC] =
{
	Hill_Block_Group(3, 3),
	Hill_Block_Group(3, 3),
	Hill_Block_Group(3, 3),
};

extern viewCoordData viewPanes[NUM_VIEW_PANES];

/* Drawing Code */
void draw()
{
    /* Clear The Screen */
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    /* Move Left 1.5 Units And Into The Screen 6.0 */
    glLoadIdentity();

    glTranslatef(-10, -2, -7);
	
	glTranslatef(viewPanes[CURRENT_VIEW_MODE].transx, 
				 viewPanes[CURRENT_VIEW_MODE].transy,
				 viewPanes[CURRENT_VIEW_MODE].transz);
	glRotatef(viewPanes[CURRENT_VIEW_MODE].rotx, 1.0f, 0.0f, 0.0f);
	glRotatef(viewPanes[CURRENT_VIEW_MODE].roty, 0.0f, 1.0f, 0.0f);
	glRotatef(viewPanes[CURRENT_VIEW_MODE].rotz, 0.0f, 0.0f, 1.0f);
	
	
	//glTranslatef(usertranslatex, usertranslatey, usertranslatez);
	//glRotatef(userrotatex, 1.0f, 0.0f, 0.0f);
	//glRotatef(userrotatey, 0.0f, 1.0f, 0.0f);
	//glRotatef(userrotatez, 0.0f, 0.0f, 1.0f);
	
	glColor4f(1.0f, 1.0f, 1.0f, 0.0f);

	
	
	glPushMatrix();
		glTranslatef(-15, -15, -10);
		drawStarWall(100);
	glPopMatrix();
	
	glPushMatrix();
		glRotatef(90, 0.0, 1.0f, 0.0);

		glTranslatef(-65, -15, -1);
	drawStarWall(100);
	glPopMatrix();
	
	glPushMatrix();
	glRotatef(-90, 0.0, 1.0f, 0.0);
	glTranslatef(0, -15, -60);
	drawStarWall(100);
	glPopMatrix();
	
	//glPushMatrix();
	//glRotatef(-90, 1.0f, 0.0f, 0.0f);
/*	
	glPushMatrix();
	glTranslatef(0, 10, 3);
	glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
	glBindTexture(GL_TEXTURE_2D, tex_id[5]);
	for(int i = 0; i < AMT_HOUSE_BLOCK_ALLOC; i++)
	{
		glPushMatrix();
		glTranslatef(0, -(i*2), 0);
		House_Blocks[i].draw();
		glPopMatrix();
	}
	glTranslatef(3.0f, 15.0f, 0.0f);
	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
	drawDoor();
	glPopMatrix();
*/	
	
	/*
	glPushMatrix();
	glTranslatef(-15, -15, -10);
	glTranslatef(60, 0, 60);
	glRotatef(-180, 0, 1.0f, 0.0f);
	drawStarWall(100);
	glPopMatrix();
	*/
	glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
	for(int i = 0; i < NUM_ALLOCATED_BLOCKS; i++)
	{
		glPushMatrix();
		glBindTexture(GL_TEXTURE_2D, tex_id[i]);
		glTranslatef(0, -(i*2), 0);
		Blocks[i].draw();
		glPopMatrix();
	}
	

//	glBindTexture(GL_TEXTURE_2D, tex_id[0]);
	//	Blocks[0].draw();	
	
	//Hill_Block_Group BLOCK(30, 30);
//	BLOCK.draw();
		
	
    /* Draw it to the screen */
    SDL_GL_SwapBuffers( );
}

void drawStarWall(int length)
{
	for(int dist = 0; dist < length; dist++) {
    	glPushMatrix();
    	glTranslatef(dist, 0.0f, 0.0f);
    	StarLine starline(25, 100, true, 1.0f);
    	glPushMatrix();
    	starline.draw();
    	glPopMatrix();
    	glPopMatrix();
	}
}

void drawDoor()
{
	glBindTexture(GL_TEXTURE_2D, tex_id[6]);
	
	static GLfloat vertices_and_tex_coords[] = 
	{
		1.0f, 0.0f,
		1.0f, 1.0f,
		0.0f, 1.0f,
		0.0f, 0.0f
	};
	static GLfloat normals[] =
	{
		0.0f, 0.0f, 1.0f,
		0.0f, 0.0f, 1.0f,
		0.0f, 0.0f, 1.0f,
		0.0f, 0.0f, 1.0f
	};
	
	glTexCoordPointer(2, GL_FLOAT, 0, vertices_and_tex_coords);
	glVertexPointer(2, GL_FLOAT, 0, vertices_and_tex_coords);
	glDrawArrays(GL_QUADS, 0, 4);
}
#endif
